<?php


namespace app\backend\models;

use App\common\models\BaseModel;
use Illuminate\Support\Facades\DB;

class BigWheelModel extends BaseModel
{
    const table = 'activity_bigwheel';

    const _field = ['*'];

    /**
     * 抽奖模式：
     * 1.九宫格（最多8种商品）
     * 2.十六宫格（最多12种商品）
     * 3.大转盘（最多8种商品）
     * 4.老虎机（3-20种奖品）
     */
    const NINE_PALACE_GRID = 1;
    const SIXTEEN_PALACE_GRID = 2;
    const BIG_WHEEL = 3;
    const SLOT_MACHINE = 4;
    public static $Lottery_Mode_Type = [
        self::NINE_PALACE_GRID,
        self::SIXTEEN_PALACE_GRID,
        self::BIG_WHEEL,
        self::SLOT_MACHINE
    ];
    public static $Lottery_Mode = [
        self::NINE_PALACE_GRID => ['min' => 1, 'max' => 8],
        self::SIXTEEN_PALACE_GRID => ['min' => 1, 'max' => 12],
        self::BIG_WHEEL => ['min' => 1, 'max' => 8],
        self::SLOT_MACHINE => ['min' => 3, 'max' => 20],
    ];

    static $_instance;

    public static function getInstance()
    {
        if (!(self::$_instance instanceof self)) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }

    /**
     * 获取数据
     * @param $condition
     * @param $page
     * @param $limit
     * @param $field
     * @return mixed
     */
    public function getList($condition, $page, $limit, array $field = self::_field)
    {
        return DB::table(self::table)->select($field)->where($condition)->apiPaginate($limit, ['*'], 'page', $page)->toarray();
    }


    /**
     * 获取数据
     * @param $condition
     * @param $field
     * @return
     */
    public function getInfo($condition, array $field = self::_field)
    {
        return DB::table(self::table)->select($field)->where($condition)->first();
    }


    /**
     * 保存数据
     * @param $data
     * @return bool
     */
    public function addSave($data)
    {
        if (!$data) {
            return '';
        }
        $data['create_time'] = time();
        return DB::table(self::table)->insertGetId($data);
    }

    /**
     * 修改数据
     * @param $condition
     * @param $data
     * @return int|string
     */
    public function updateSave($condition, $data)
    {
        if (!$condition || !$data) {
            return '';
        }
        $data['update_time'] = time();
        return DB::table(self::table)->where($condition)->update($data);
    }

    /**
     * 删除数据
     * @param $condition
     * @return int|string
     */
    public function deleteData($condition)
    {
        if (!$condition) {
            return '';
        }
        return DB::table(self::table)->where($condition)->update(['is_delete'=>1]);

        try {
            DB::beginTransaction();
            //删除活动
            DB::table(self::table)->where($condition)->delete();
            //删除活动下奖品
            DB::table('activity_bigwheel_prize')->where($condition)->delete();
            //商城活动参与记录
            DB::table('bigwheel_log')->where($condition)->delete();
            DB::commit();
            return array('state' => 200, 'msg' => 'success');
        } catch (Exception $e) {
            DB::rollback();
            return array('state' => false, 'msg' => $e->getMessage());
        }

    }


}
